package com.by_man.presenter.module_gestureLock;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.repository.IpAddress;
import com.by_man.repository.UserInfos;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by man on 2017-03-17.
 */
public class LoginPasswordVerifyActivity extends AppCompatActivity {
    private RequestQueue rQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_password_verify);
        rQueue = Volley.newRequestQueue(this);
        rQueue.start();
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, ModifyGestureLockVerifyActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, ModifyGestureLockVerifyActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode,event);
    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        final EditText sscode=(EditText)findViewById(R.id.setting_sscode);
        sscode.setTransformationMethod(PasswordTransformationMethod.getInstance());
        final Button setting_sscode_save=(Button)findViewById(R.id.setting__sscode_save);
        final CheckBox check_sscode= (CheckBox) findViewById(R.id.check_sscode);
        //是否显示密码
        check_sscode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(check_sscode.isChecked()){
                    sscode.setTransformationMethod(HideReturnsTransformationMethod.getInstance());  //密码以明文显示

                }else{
                    sscode.setTransformationMethod(PasswordTransformationMethod.getInstance());

                }
            }
        });

        setting_sscode_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = sscode.getText().toString().trim();
                password = SharedApplication.MD5("$20160608$" + password, 32);
                String url = "http://" + IpAddress.ServerIP + "/api/confirm/password/data.php";
                HashMap<String,String> params = new HashMap<String, String>();
                params.put("token", UserInfos.token);
                params.put("password", password);
                JSONObject jsonObject = new JSONObject(params);
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                        url, jsonObject,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                try {
                                    boolean isTrue = jsonObject.getBoolean("isTrue");
                                    if (isTrue) {
                                        Toast.makeText(LoginPasswordVerifyActivity.this, "原始密码正确", Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(LoginPasswordVerifyActivity.this, ModifyGestureLockActivity.class);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        startActivity(intent);
                                    } else {
                                        Toast.makeText(LoginPasswordVerifyActivity.this, "原始密码错误", Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException je) {
                                    je.printStackTrace();
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Log.e("LoginPWVerifyError",volleyError.getMessage(),volleyError);
                            }
                        });
                rQueue.add(jsonObjectRequest);
            }
        });
    }
}
